//
// Created by yanhai on 2020/4/1.
//

#include <string>
#include "server/http_server.hpp"

/**
 * test:
 *  curl --location -X POST 'http://127.0.0.1:8000/v1/test' \
--form 'aa=中文测试' \
--form 'bb=bb1' \
--form 'cc=cc1' \
--form 'cc=cc2' \
--form 'ee=ee' \
--form 'file=@/root/20200401175738.png'
 * @param request
 * @param response
 * @param arg
 */
static void test(const Request *request, Response *response)
{
    std::string res;
    auto aa = request->form().get("aa");
    auto bb = request->form()["bb"];
    auto cc1 = request->form()["cc"];
    auto cc = request->form().getlist("cc");
    auto ee = request->form().get("ee", "exx");

    res = "aa:";
    res += aa;
    res += "; bb:";
    res += bb;
    res += "; cc:";
    for (auto &item: cc) {
        res += item + ",";
    }
    res += "; ee:";
    res += ee;

    auto files = request->file().getlist("file");
    assert(files.size() == 1);
    auto &file = files[0];
    res += "   filename:" + file.filename();
    file.save("/tmp/" + file.filename());

    response->write(res);
}

int main()
{
    auto &httpServer = HTTPServer::instance();
    httpServer.set_cb("/v1/test", test, EVHTTP_REQ_POST);
    httpServer.listen(8000, "0.0.0.0");
    httpServer.start();
    return 0;
}
